package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.DesignerDaolmpl;
import com.ysd.entity.Designer;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminDesignerServlet extends BaseServlet {
	/**
	 * 鑾峰彇鍒嗙被
	 * localhost:8080/kouhong/adminDesigner?reqName=getDesigners*/
      
	
	public void getDesigners(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 DesignerDaolmpl ddDao=new DesignerDaolmpl();
	 int page=Integer.parseInt(request.getParameter("page"));
	 int size=Integer.parseInt(request.getParameter("size"));
	 List<Designer> ddList=ddDao.getDesignerByPage(page, size);
	 
	 response.setCharacterEncoding("UTF-8");
	 PrintWriter out=response.getWriter();

	 Gson gson=new Gson();
	 String res=gson.toJson(ddList);
	 out.print(res);
	
	 
	 out.flush();
	 out.close();
	}
	public void getAllDesigners(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 DesignerDaolmpl ddDao=new DesignerDaolmpl();
		
		 List<Designer> ddList=ddDao.getDesignerList();
		 
		 response.setCharacterEncoding("UTF-8");
		 PrintWriter out=response.getWriter();

		 Gson gson=new Gson();
		 String res=gson.toJson(ddList.size());
		 
		 out.print(res);
		
		 
		 out.flush();
		 out.close();
		}

		/**鍒犻櫎鍒嗙被
		 * localhost:8080/kouhong/adminDesigner?reqName=delDesigner
		 * */
	public void delDesigner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//鎺ュ彈鍓嶅彴浼犳潵鐨勫弬鏁�
		int delId=Integer.parseInt(request.getParameter("delId"));
		DesignerDaolmpl ddDao=new DesignerDaolmpl();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		int n=ddDao.delDesignerById(delId);//璋冪敤瀹炵幇绫荤殑鍒犻櫎鏂规硶銆�
		if(n>0) {
			out.print(Result.toClient(true, "鍒犻櫎鎴愬姛", ""));
		}else {
			out.print(Result.toClient(false, "鍒犻櫎澶辫触", ""));
		}
		  out.flush();
		    out.close();

	}

	/**鍒犻櫎鍒嗙被
	 * localhost:8080/kouhong/adminDesigner?reqName=addDesigner
	 * */
	public void addDesigner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		DesignerDaolmpl ddDao=new DesignerDaolmpl();
	   String designerName=request.getParameter("designerName");
	   String designerTitle=request.getParameter("designerTitle");
	   String designerImg=request.getParameter("imgs");
	   String designerContent=request.getParameter("designerContent");
	   String designerContentImg=request.getParameter("img");
	   String designerCompanyContent=request.getParameter("designerCompanyContent");
	   String designerCompanyImg=request.getParameter("imge");
		
		
		Designer de=new Designer(0, designerName, designerTitle, designerImg, designerContent, designerContentImg, designerCompanyContent, designerCompanyImg);
		System.out.println(de);
	
		int n=ddDao.addDesigner(de);
		if (n>0) {
			out.print(Result.toClient(true, "娣诲姞鎴愬姛", ""));
		} else {
				out.print(Result.toClient(false, "娣诲姞澶辫触", ""));
		}
	out.flush();
	out.close();
}
  
	public void uploadImgs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		// 璁剧疆缂栫爜鏍煎紡
		PrintWriter out = response.getWriter(); 
		// 1.鍒涘缓SmartUpload
		SmartUpload smart = new SmartUpload(); 
		// 2.璁剧疆瀛楃闆�
		smart.setCharset("UTF-8"); 
		// 3.鍒濆鍖�
		smart.initialize(getServletConfig(), request, response);
		
		try { // 4.涓婁紶鏂囦欢 smart.upload(); // 5.淇濆瓨鏂囦欢鍒版寚瀹氫綅缃�(save鍙彁渚涜矾寰勶紝浣跨敤鍘熸枃浠跺悕涓婁紶鏂囦欢)
				// 
			smart.upload();
			// 5.淇濆瓨鏂囦欢鍒版寚瀹氫綅缃�(saveAs鍙互鐢ㄦ寚瀹氬悕绉颁笂浼犳枃浠�) //
				// 
			String filename= System.currentTimeMillis()+".jpg";
			smart.getFiles().getFile(0).saveAs("/uploadImgs/designer/"+filename);
				// // 鑾峰彇鏂囦欢鍚�
			filename = "/uploadImgs/designer/"+filename;
			out.print(Result.toClient(true, "涓婁紶鎴愬姛",filename));
		} catch (SmartUploadException e) {
			out.print(Result.toClient(false, "涓婁紶澶辫触", "鍥剧墖杩囧ぇ"));
			e.printStackTrace();
		}
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/kouhong/adminDesigner?reqName=getDesigner
	 * */
	public void getDesigner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		DesignerDaolmpl ddDao=new DesignerDaolmpl();
		int deId=0;
		if(request.getParameter("designerId")!=null && !request.getParameter("designerId").equals("")) {
			deId=Integer.parseInt(request.getParameter("designerId"));
		}
	
		Designer d = ddDao.getDesignerbById(deId);
		Gson gson = new Gson();
		out.print(gson.toJson(d));
	out.flush();
	out.close();
}
	
	/**
	 * localhost:8080/kouhong/adminDesigner?reqName=updateDesigner
	 * */
	public void updateDesigner(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		DesignerDaolmpl ddDao=new DesignerDaolmpl();
		int designerId = 0;
		if(request.getParameter("designerId")!=null && !request.getParameter("designerId").equals("")) {
			designerId = Integer.parseInt(request.getParameter("designerId"));	
		}
		String designerName = request.getParameter("designerName");
		String designerTitle = request.getParameter("designerTitle");
		String designerImg = request.getParameter("designerImg");
		String designerContent = request.getParameter("designerContent");
		String designerContentImg = request.getParameter("designerContentImg");
		String designerCompanyContent = request.getParameter("designerCompanyContent");
		String designerCompanyImg = request.getParameter("designerCompanyImg");
		Designer d = new Designer(designerId, designerName, designerTitle, designerImg, designerContent, designerContentImg, designerCompanyContent, designerCompanyImg);		
		 System.out.println(d);
		int n = ddDao.updateDesigner(d);
		
		if (n>0) {
			out.print(Result.toClient(true, "淇敼鎴愬姛", ""));
		} else {
			out.print(Result.toClient(false, "淇敼澶辫触", ""));
		}
	out.flush();
	out.close();
}
	/**
	 * 批量删除
	 * @param localhost:8080/kouhong/adminDesigner?reqName=deleteDesignerById
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deleteDesignerById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 DesignerDaolmpl ddDao=new DesignerDaolmpl();
		 response.setCharacterEncoding("UTF-8");
		 PrintWriter out=response.getWriter();
		 String desId = request.getParameter("designerId");
		int n = ddDao.deleteDesignerById(desId);
		
		 if (n>0) {
			 out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		 out.flush();
		 out.close();
		}
}



